#include "precompiled.h"

#include <stdio.h>
#include <tchar.h>
#include <iostream>

#include "CUtils.h"
#include "CDBUpdater.h"
#include "CScanner.h"


const char * const kBaseCmd = "-b";
const char * const kUpdateBaseCmd = "-u";
const char * const kPublicKeyCmd = "-k";
const char * const kInCmd = "-in";

void showHelp()
{
	std::cout << "Usage: DBProcessor.exe update -b current_base -u new_base -k public_key_PEM" << std::endl;
	std::cout << "Usage: DBProcessor.exe scan -in file_or_folder -b base -k public_key_PEM" << std::endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	//CScanner::scan("DBGeneratorSignatures", "base.xml", "pub_key.pem");

	if( argc < 8 )
	{
		showHelp();
		return -1;
	}

	if ( !strcmp("update", argv[1]) )
	{
		std::string base_filename;
		std::string upd_base_filename;
		std::string public_key;

		if ( !strcmp(argv[2], kBaseCmd) ) base_filename = argv[3];
		else if ( !strcmp(argv[2], kUpdateBaseCmd) ) upd_base_filename = argv[3];
		else if ( !strcmp(argv[2], kPublicKeyCmd) ) public_key = argv[3];

		if ( !strcmp(argv[4], kBaseCmd) ) base_filename = argv[5];
		else if ( !strcmp(argv[4], kUpdateBaseCmd) ) upd_base_filename = argv[5];
		else if ( !strcmp(argv[4], kPublicKeyCmd) ) public_key = argv[5];

		if ( !strcmp(argv[6], kBaseCmd) ) base_filename = argv[7];
		else if ( !strcmp(argv[6], kUpdateBaseCmd) ) upd_base_filename = argv[7];
		else if ( !strcmp(argv[6], kPublicKeyCmd) ) public_key = argv[7];

		if ( !CDBUpdater::update(base_filename.c_str(), upd_base_filename.c_str(), public_key.c_str()) )
		{
			std::cout << "The update has been failed." << std::endl;
			return -1;
		}

		std::cout << "The base has been updated." << std::endl;
	}
	else if ( !strcmp("scan", argv[1]) )
	{
		std::string base_filename;
		std::string in;
		std::string public_key;

		if ( !strcmp(argv[2], kBaseCmd) ) base_filename = argv[3];
		else if ( !strcmp(argv[2], kInCmd) ) in = argv[3];
		else if ( !strcmp(argv[2], kPublicKeyCmd) ) public_key = argv[3];

		if ( !strcmp(argv[4], kBaseCmd) ) base_filename = argv[5];
		else if ( !strcmp(argv[4], kInCmd) ) in = argv[5];
		else if ( !strcmp(argv[4], kPublicKeyCmd) ) public_key = argv[5];

		if ( !strcmp(argv[6], kBaseCmd) ) base_filename = argv[7];
		else if ( !strcmp(argv[6], kInCmd) ) in = argv[7];
		else if ( !strcmp(argv[6], kPublicKeyCmd) ) public_key = argv[7];

		CScanner::scan(in.c_str(), base_filename.c_str(), public_key.c_str());
	}
	else
	{
		showHelp();
		return -1;
	}

	return 0;
}